<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Models\Admin\User;
use App\Http\Controllers\Controller;
use App\Http\Models\Admin\Node;
require_once "extend\org\Verify.php";
class LoginController extends Controller
{
    public function index() {
		if(session('uid')){
			$indexCtrl = new IndexController();
			return $indexCtrl->index();
		}else{
			return view('admin.login');
		}
	}

	public function login() {
		$param = $_POST;
		// 判断验证码
		$verify = new \Verify();
        if (!$verify->check($param['code'])) {
            return ['code'=>'0','msg'=>'验证码错误'];
        }
		// 判断用户名
		$userModel = new User();
		$user = $userModel->getOneUser(['user_name'=>$param['username']]);
		if(empty($user)){
			return ['code'=>'0','msg'=>'该用户不存在'];
		}
		// 判断密码
		if($user['passwd'] != md5($param['passwd'].config('app.key'))){
			return ['code'=>'0','msg'=>'密码错误'];
		}
		$this->doLogin($user);
		return ['code'=>'1','msg'=>'success'];
	}

	public function doLogin($user) {
		$data = [
			'uid'			=> $user['id'],
			'user_info'		=> $user,
			'last_access'	=> time()
		];
		session($data);
		$nodeModel = new Node();
		session(['menu' => $nodeModel->mkMenu()]);
	}

	public function logout() {
		session()->flush();
		redirect('/admin')->send();
	}

	public function checkVerify(){
		$fontttfArr = [2,4,6];
		$fontttf = $fontttfArr[array_rand($fontttfArr,1)].".ttf";
        $verify = new \Verify();
        $verify->imageH = 40;
        $verify->imageW = 120;
        $verify->length = 4;
        $verify->useNoise = false;
		$verify->useCurve = true;
		$verify->fontttf = $fontttf;
        $verify->fontSize = 18;
        return $verify->entry();
    }
}
